<?php

class EventManager
{
	private $_db;
	
	public function __construct(PDO $db)
	{
		$this->setDb($db);
	}
	
	public function setDb($db)
	{
		$this->_db = $db;
	}
	
	public function add(Event $event)
	{
		//Fonction qui va ajouter un sportsEvent dans la table sportsevents
		$reqInsert = $this->_db->prepare("INSERT INTO events SET fullName= :fullName, shortName= :shortName, startDate= :startDate, endDate= :endDate, sportsEvent_id= :sportsEvent_id");
		$reqInsert->bindValue(":fullName", $event->fullName());
		$reqInsert->bindValue(":shortName", $event->shortName());
		$reqInsert->bindValue(":startDate", $event->startDate());
		$reqInsert->bindValue(":endDate", $event->endDate());
		$reqInsert->bindValue(":sportsEvent_id", $event->sportsEvent());
		$reqInsert->execute();
		$event->hydrate(array(
			'id' => $this->_db->lastInsertId()
		));
	}
	public function delete(Event $event)
	{
		//Fonction qui va supprimer un sportsEvent dans la table sportsevents
		$reqDelete = $this->_db->prepare("DELETE FROM events WHERE id= :id");
		$reqDelete->bindValue(":id", $event->id());
		$reqDelete->execute();
	}
	public function update(Event $event)
	{
		//Fonction qui va mettre à jour un sportsEvent dans la table sportsevents
		$reqUpdate = $this->_db->prepare("UPDATE events SET fullName= :fullName, shortName= :shortName, startDate= :startDate, endDate= :endDate, sport_id= :sport_id WHERE id= :id");
		$reqUpdate->bindValue(":id", $event->id(), PDO::PARAM_INT);
		$reqUpdate->bindValue(":fullName", $event->fullName(), PDO::PARAM_STR);
		$reqUpdate->bindValue(":shortName", $event->shortName(), PDO::PARAM_STR);
		$reqInsert->bindValue(":startDate", $event->startDate(), PDO::PARAM_STR);
		$reqInsert->bindValue(":endDate", $event->endDate(), PDO::PARAM_STR);
		$reqUpdate->bindValue(":sportsEvent_id", $event->sportsEvent(), PDO::PARAM_INT);
		$reqUpdate->execute();
	}
	
	
	public function getLast()
	{
		//Fonction qui ramène le dernier event créé = celui avec l'id le plus grand
		$reqSelect = $this->_db->prepare("SELECT * FROM events WHERE id= :id");
		$id = $this->_db->lastInsertId();
		$reqSelect->bindValue(":id", $id);
		$reqSelect->execute();
		$event = $reqSelect->fetch(PDO::FETCH_ASSOC);
		return new Event($event);
	}
	
	public function get($search)
	{
		//Fonction qui va chercher un event dans la table events à partir du critère de recherche search
		if(is_int($search))
		{
			$reqSelect = $this->_db->prepare("SELECT * FROM events WHERE id= :id");
			$reqSelect->bindValue(":id", $search);
			$reqSelect->execute();
			$event = $reqSelect->fetch(PDO::FETCH_ASSOC);
			return new Event($event);
		}
		if(is_string($search))
		{
			$reqSelect = $this->_db->prepare("SELECT * FROM events WHERE fullName LIKE %:fullName% OR shortName LIKE %:shortName%");
			$reqSelect->bindValue(":fullName", $search);
			$reqSelect->bindValue(":shortName", $search);
			$reqSelect->execute();
			$event = $reqSelect->fetch(PDO::FETCH_ASSOC);
			return new Event($event);
		}
	}
	public function existe($search)
	{
		//Fonction qui vérifie l'existence d'un event dans la table events
		if(is_int($search))
		{
			$reqSelect = $this->_db->prepare("SELECT * FROM events WHERE id= :id");
			$reqSelect->bindValue(":id", $search);
			$reqSelect->execute();
			return (bool) $reqSelect->fetch();
		}
		if(is_string($search))
		{
			$reqSelect = $this->_db->prepare("SELECT * FROM events WHERE fullName= :fullName OR shortName= :shortName");
			$reqSelect->bindValue(":fullName", $search);
			$reqSelect->bindValue(":shortName", $search);
			$reqSelect->execute();
			return (bool) $reqSelect->fetch();
		}
	}
	public function getListEvents()
	{
		//Fonction qui retourne la liste des events
		$events = array();
		$reqListe = $this->_db->query("SELECT * FROM events WHERE startDate < CURDATE() AND endDate > CURDATE()");
		while($event = $reqListe->fetch(PDO::FETCH_ASSOC))
		{
			$eventsL = new Event($event);
			$events[] = $eventL;
		}
		$reqListe->closeCursor();
		return $events;
	}
	
	public function getListRounds(Sport $sport, Event $event)
	{
		//Fonction qui retourne la liste des différents rounds d'un event
		$rounds=array();
		switch($sport->id())
		{
			case 1:
				$reqListe = $this->_db->query("SELECT DISTINCT(round) FROM scheduleevent_basket WHERE event_id=".$event->id()."");
				echo "SELECT DISTINCT(round) FROM scheduleevent_basket WHERE event_id=".$event->id()."";
				while($round = $reqListe->fetch(PDO::FETCH_ASSOC))
				{
					$rounds[] = $round['round'];
				}
				$reqListe->closeCursor();
				return $rounds;
			default:
				return;
		}
	}

}